package com.ray.dao;

import com.ray.pojo.User;
import com.ray.utils.DbUtil;

import java.sql.Connection;
import java.sql.ResultSet;

public class UserDaoImpl implements UserDao
{
    @Override
    public ResultSet getUserById(long id)
    {
        // 声明结果集对象
        ResultSet resultSet = null;

        // 创建连接
        Connection connetion = DbUtil.getConnetion();

        // 编写sql语句
        String sql = "SELECT ID, NAME, GENDER FROM USER WHERE ID = ?";

        // 设置sql语句中的参数
        Object[] params = {id};

        try
        {
            // 执行查询
            resultSet = DbUtil.query(connetion, sql, params);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        // 返回结果集
        return resultSet;
    }

    @Override
    public ResultSet getAllUser()
    {
        // 声明结果集对象
        ResultSet resultSet = null;

        // 创建连接
        Connection connetion = DbUtil.getConnetion();

        // 编写sql语句
        String sql = "SELECT ID, NAME, GENDER FROM USER";

        Object[] params = {};

        try
        {
            // 执行查询
            resultSet = DbUtil.query(connetion, sql, params);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        // 返回结果集
        return resultSet;
    }

    @Override
    public int addUser(User user)
    {

        int result = 0;
        // 声明结果集对象
        ResultSet resultSet = null;

        // 创建连接
        Connection connetion = DbUtil.getConnetion();

        // 编写sql语句
        String sql = "insert into user (NAME, GENDER) values (?, ?);";

        Object[] params = {user.getName(), user.getGender()};

        try
        {
            // 执行更新
            result = DbUtil.execute(connetion, sql, params);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        // 返回结果集
        return result;
    }

    @Override
    public int updateUser(User user)
    {
        int result = 0;
        // 声明结果集对象
        ResultSet resultSet = null;

        // 创建连接
        Connection connetion = DbUtil.getConnetion();

        // 编写sql语句
        String sql = "update user set NAME = ?, GENDER = ? where ID = ?";

        Object[] params = {user.getName(), user.getGender(), user.getId()};

        try
        {
            // 执行更新
            result = DbUtil.execute(connetion, sql, params);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        // 返回结果集
        return result;
    }

    @Override
    public int deleteUser(int id)
    {
        int result = 0;

        // 创建连接
        Connection connetion = DbUtil.getConnetion();

        // 编写sql语句
        String sql = "delete from user where ID = ?";

        Object[] params = {id};

        try
        {
            // 执行更新
            result = DbUtil.execute(connetion, sql, params);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
        // 返回结果集
        return result;

    }
}
